# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/topics/item-pipeline.html

import config
import shutil, os, errno

import config, dao

from flash4399.items import FlashItem, FlashCItem

from scrapy.contrib.pipeline.media import MediaPipeline
from scrapy.core.exceptions import DropItem
from scrapy.http import Request
from scrapy import log


class FlashStorePipeline(object):
    def process_item(self, spider, item):

        if isinstance(item, FlashItem):

            print 'flash %s' % (item['id'],)

            dao.insert_row('4399', item)

            # downloader.download('4399', 'img', item['id'], item['pic'])
            # downloader.download('4399', 'flash', item['id'], item['flash'])

        elif isinstance(item, FlashCItem):

            print 'flashc %s' % (item['id'],)
            # downloader.download('4399', 'imgs', item['id'], item['pic_small'])

        else:
            raise DropItem("bad item instance")

        return item




class ResDownloadPipeline(MediaPipeline):

    def get_media_requests(self, item, info):

        if isinstance(item, FlashItem):

            return Request(item['pic'])
            # yield Request(item['flash'])

        elif isinstance(item, FlashCItem):

            return Request(item['pic_small'])


    def item_completed(self, results, item, info):
        # print results
        # request = info['request']
        # print request
        # if not image_paths:
        #     raise DropItem("Item contains no images")
        # 

        for success, response in results:
            if success:
                if isinstance(item, FlashItem):
                    if response.url == item['flash']:
                        self.save_file(response.body, item['id'], 'swf')
                    elif response.url == item['pic']:
                        self.save_file(response.body, item['id'], 'img')
                elif isinstance(item, FlashCItem):
                    if response.url == item['pic_small']:
                        self.save_file(response.body, item['id'], 'imgs')
            else:
                print response
                log.msg('cannot download file ')

        return item


    def save_file(self, data, id, type):
        id = str(id)
        if len(id) == 1:
            id = '0' + id
    
        path = config.DOWNLOAD_DIR + '4399/' + str(id)[-2:] + '/'
    
        try:
            os.makedirs(path)
        except OSError, e:
            if e.errno != errno.EEXIST:
                raise e
    
        dst_file = ''
        if type == 'swf':
            dst_file = path + id + '.swf'
        elif type == 'img':
            dst_file = path + id + '.jpg'
        elif type == 'imgs':
            dst_file = path + id + '_s.jpg'
        else:
            raise Exception('bad download type')

        log.msg('downlaod file %s,%s to %s' % (type, id, dst_file))        
    
        tfile = open(dst_file, 'wb')
        tfile.write(data)
        tfile.close()



